import router from "./router";
import store from "./store";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
const whiteList = ["/login", "/404"];
router.beforeEach(async (to, from, next) => {
  // 开启进度条
  NProgress.start();
  if (store.getters.token) {
    // 存在token
    if (to.path === "/login") {
      next("/");
      // 关闭进度条
      NProgress.done();
    } else {
      if (!store.getters.userId) {
        await store.dispatch("user/getUserInfo");
      }
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) {
      next();
    } else {
      next("login"); //中转到登录页面
      NProgress.done(); // 关闭进度条
    }
  }
});
/* 后置守卫 */
router.afterEach(() => {
  NProgress.done();
});
